local extension = Package("luming_hegemon")
extension.extensionName = "luming"
extension.game_modes_whitelist = {"lm_mode"}
Fk:loadTranslationTable{
  ["luming_hegemon"] = "鹿鸣-霸道",
}

local U = require "packages/utility/utility"
local LM = require "packages/luming/LM"

--------------------------------------------------曹操-------------------------------------------------------------
local lm_weiwu = fk.CreateTriggerSkill{
  name = "lm_weiwu",
  mute = true,
  events = {fk.GameStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local svts = table.simpleClone(self.lmLordList)
    -- table.shuffle(svts)
    local max = 3
    local num = 1

    svts = table.map(svts, function(s) return LM.initServant(player, s) end)
    LM.setServantsPool(player, svts)
    LM.setServantsMax(player, max)
    local init_svts = table.random(LM.getServantsPool(player), num)
    LM.findServants(player, table.map(init_svts, Util.NameMapper), self.name)
  end
}
local lm_qiuxian = fk.CreateActiveSkill{
  name = "lm_qiuxian",
  anim_type = "offensive",
  prompt = "#lm_qiuxian",
  card_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and LM.canFindNewSvt(player)
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and not Self:prohibitDiscard(Fk:getCardById(to_select))
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = effect.cards
    room:throwCard(cards, self.name, player, player)
    LM.findServantsByChc(player,1,1,4, self.name)
  end
}
local lm_woyong = fk.CreateViewAsSkill{
  name = "lm_woyong",
  mute = true,
  pattern = ".",
  prompt = function(self, selected_cards, selected)
    local g = Fk.generals[self.interaction.data]
    if not g then
      return "#lm_woyong-prompt"
    end
    local skill ---@type ViewAsSkill
    for _, s in ipairs(g.related_skills) do
      if s.trueName == "lm_caiyong" then
        skill = s
        break
      end
    end
    if not skill then
      return "#lm_woyong-prompt"
    end
    return "#lm_woyong-promptX:::" .. g.name .. ":" .. Fk:translate(":" .. skill.name)
  end,
  interaction = function()
    local alls = {}
    local alls_name = {}
    local names = {}
    LM.getServantsByRule(Self, function(svt)
      local g = Fk.generals[svt.name]
      if not g then return end
      return table.find(g.related_skills, function(s)
        if s.trueName == "lm_caiyong" then
          local card = s:viewAs({})
          if card then
            table.insert(alls, card.name)
          else
            table.insert(alls, s.pattern)
          end
          table.insert(alls_name, svt.name)
        end
      end)
    end)
    for i, name in ipairs(alls) do
      if Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(Fk:cloneCard(name)) or (Fk.currentResponsePattern == nil and Self:canUse(Fk:cloneCard(name))) then
        table.insertIfNeed(names, alls_name[i])
      end
    end
    return UI.ComboBox {choices = names, all_choices = alls_name}
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local g = Fk.generals[self.interaction.data]
    if not g then return end
    local skill ---@type ViewAsSkill
    for _, s in ipairs(g.related_skills) do
      if s.trueName == "lm_caiyong" then
        skill = s
        break
      end
    end
    if not skill then return end
    local card = skill:viewAs({})
    if not card then return end
    card.extra_data = card.extra_data or {}
    card.extra_data.lm_woyong_svt = g.name
    return card
  end,
  before_use = function(self, player, use)
    local card = use.card
    if card and card.extra_data and card.extra_data.lm_woyong_svt then
      if #LM.removeServants(player, {card.extra_data.lm_woyong_svt}, player, nil, self.name) == 0 then return "" end
    end
  end,
  after_use = function(self, player, use)
    local room = player.room
    if #player.player_cards[Player.Hand] < 3 then
      room:drawCards(player,1,self.name)
    end
  end,
  enabled_at_play = function(self, player)
    for _, svt in ipairs(LM.getServants(player)) do
      local g = Fk.generals[svt.name]
      if not g then return end
      for _, s in ipairs(g.related_skills) do
        if s.trueName == "lm_caiyong" then
          if s:enabledAtPlay(player) then
            return true
          end
        end
      end
    end
  end,
  enabled_at_response = function(self, player, response)
    for _, svt in ipairs(LM.getServants(player)) do
      local g = Fk.generals[svt.name]
      if not g then return end
      for _, s in ipairs(g.related_skills) do
        if s.trueName == "lm_caiyong" then
          if s:enabledAtResponse(player, response) then
            return true
          end
        end
      end
    end
  end,
}
local function woyong_response(self, player, response)
  local card = self:viewAs({}) or Fk:cloneCard(self.pattern)
  return not response and card and Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card) or (Fk.currentResponsePattern == nil and Self:canUse(card))
end
local lm__caocao = General(extension,"lm__caocao","lm_k__ba",5)
-- lm__caocao.servant = "1/3"
-- LM.setlordServantInfo("lm__caocao",lm__caocao.servant)
lm__caocao:addSkill(lm_weiwu)
lm__caocao:addSkill(lm_qiuxian)
lm__caocao:addSkill(lm_woyong)
Fk:loadTranslationTable{
  ["lm__caocao"] = "曹操",
  ["lm_weiwu"] = "魏武",
  [":lm_weiwu"] = "军力技，你的初始将领上限为3。游戏开始时，你随机获得一名将领。",
  ["lm_qiuxian"] = "求贤",
  [":lm_qiuxian"] = "阶段技，你可以弃置一张牌，然后四选一发现一名将领。",
  ["#lm_qiuxian"] = "你可以弃置一张牌，然后四选一发现一名将领",
  ["lm_woyong"] = "我用",
  [":lm_woyong"] = "你可以移除一名有〖才用〗的将领以执行之，然后若你的手牌数小于3，你摸一张牌。",
  ["#lm_woyong-prompt"] = "我用：你可以移除一名有〖才用〗的将领以执行之，然后若你的手牌数小于3，你摸一张牌。",
  ["#lm_woyong-promptX"] = "我用：你可以移除 %arg 以%arg2然后若你的手牌数小于3，你摸一张牌。",
  ["@[lm_servants]&lm_weiwu"] = "将池",
  ["@[lm_servants]lm_weiwu"] = "将领",
}

local lm_caiyong__caoren = fk.CreateViewAsSkill{
  name = "lm_caiyong__caoren",
  mute = true,
  pattern = "nullification",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("nullification")
    card.skillName = "lm_woyong"
    return card
  end,
  enabled_at_response = woyong_response
}
lm_caiyong__caoren.trueName = "lm_caiyong"
local lm_duanxie = fk.CreateTriggerSkill{
  name = "lm_duanxie",
  anim_type = "offensive",
  events = {fk.DamageFinished},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and data.from and data.from ~= player and data.to == player and data.from.phase == Player.Play and player:usedSkillTimes(self.name,Player.HistoryRound) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room.logic:getCurrentEvent():findParent(GameEvent.Phase):shutdown()
  end,
}
local lm__caoren = General(extension,"lm__caoren","lm_k__ba",0)
-- lm__caoren.cardinfo = "nullification"
lm__caoren.hidden = true
LM.setlordServant(lm_weiwu,"lm__caoren")
lm__caoren:addSkill(lm_duanxie)
lm__caoren:addRelatedSkill(lm_caiyong__caoren)
Fk:loadTranslationTable{
  ["lm__caoren"] = "曹仁",
  ["lm_duanxie"] = "断泄",
  [":lm_duanxie"] = "轮次技，当其他角色于出牌阶段内对你造成伤害后，你可以结束本阶段。",
  ["lm_caiyong__caoren"] = "才用",
  [":lm_caiyong__caoren"] = "视为使用【无懈可击】。",
}

local lm_caiyong__guojia = fk.CreateViewAsSkill{
  name = "lm_caiyong__guojia",
  mute = true,
  pattern = "archery_attack",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("archery_attack")
    card.skillName = "lm_woyong"
    return card
  end,
  enabled_at_response = woyong_response
}
lm_caiyong__guojia.trueName = "lm_caiyong"
local lm_yiji = fk.CreateTriggerSkill{
  name = "lm_yiji",
  anim_type = "offensive",
  events = {fk.Damage},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and data.to == player
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tricks = LM.GetCardByRule(room, ".|.|.|.|.|tricks", data.damage, {Card.DrawPile})
    if #tricks > 0 then
      room:obtainCard(player.id, tricks, true, fk.ReasonJustMove)
    end
  end,
}
local lm__guojia = General(extension,"lm__guojia","lm_k__ba",0)
LM.setlordServant(lm_weiwu,"lm__guojia")
-- lm__guojia.cardinfo = "archery_attack"
lm__guojia.hidden = true
lm__guojia:addSkill(lm_yiji)
lm__guojia:addRelatedSkill(lm_caiyong__guojia)
Fk:loadTranslationTable{
  ["lm__guojia"] = "郭嘉",
  ["lm_yiji"] = "遗计",
  [":lm_yiji"] = "当你受到伤害后，你可以从牌堆内获得X张锦囊牌（X为伤害值）。",
  ["lm_caiyong__guojia"] = "才用",
  [":lm_caiyong__guojia"] = "视为使用【万箭齐发】。",
}

local lm_caiyong__dianweicaoang = fk.CreateViewAsSkill{
  name = "lm_caiyong__dianweicaoang",
  mute = true,
  pattern = "peach",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("peach")
    card.skillName = "lm_woyong"
    return card
  end,
  enabled_at_response = woyong_response
}
lm_caiyong__dianweicaoang.trueName = "lm_caiyong"
local lm_dianhou = fk.CreateActiveSkill{
  name = "lm_dianhou",
  anim_type = "offensive",
  prompt = "#lm_dianhou",
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= Self.id and not Fk:currentRoom():getPlayerById(to_select):isNude()
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    local card = room:askForCardChosen(player, to, "he", self.name)
    room:throwCard(card, self.name, to, player)
    LM.removeServants(player,{"lm__dianweicaoang"}, player, nil, self.name)
    local getride
    for _, cid in ipairs(room.draw_pile) do
      local card = Fk:getCardById(cid)
      if card.type == Card.TypeEquip and (card.sub_type == Card.SubtypeDefensiveRide) then
        getride = cid
        break
      end
    end
    if getride then
      room:obtainCard(player.id,getride,true,fk.ReasonJustMove)
      local ride = Fk:getCardById(getride)
      if player:canUse(ride) then
        room:useCard({
          card = ride,
          from = player.id,
          tos = {{player.id}},
          skillName = self.name,
          extraUse = true,
        })
      end
    end
  end
}
local lm__dianweicaoang = General(extension,"lm__dianweicaoang","lm_k__ba",0)
-- lm__dianweicaoang.cardinfo = "peach"
lm__dianweicaoang.hidden = true
LM.setlordServant(lm_weiwu,"lm__dianweicaoang")
lm__dianweicaoang:addSkill(lm_dianhou)
lm__dianweicaoang:addRelatedSkill(lm_caiyong__dianweicaoang)
Fk:loadTranslationTable{
  ["lm__dianweicaoang"] = "典韦曹昂",
  ["lm_dianhou"] = "殿后",
  [":lm_dianhou"] = "出牌阶段，你可以弃置一名其他角色一张牌并移除此将领，然后你从牌堆内获得一张防御坐骑牌使用之。",
  ["#lm_dianhou"] = "你可以弃置一名其他角色一张牌并移除典韦曹昂，然后你从牌堆内获得一张防御坐骑牌使用之",
  ["lm_caiyong__dianweicaoang"] = "才用",
  [":lm_caiyong__dianweicaoang"] = "视为使用【桃】。",
}

local lm_caiyong__yuejin = fk.CreateViewAsSkill{
  name = "lm_caiyong__yuejin",
  mute = true,
  pattern = "dismantlement",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("dismantlement")
    card.skillName = "lm_woyong"
    return card
  end,
  enabled_at_response = woyong_response
}
lm_caiyong__yuejin.trueName = "lm_caiyong"
local lm_xiandeng = fk.CreateTriggerSkill{
  name = "lm_xiandeng",
  anim_type = "offensive",
  events = {fk.TurnStart},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target and player ~= target
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local disjink = room:askForDiscard(player, 1, 1, false, self.name, true, "jink|.|.|.|.|", "#lm_xiandeng_disjink:" .. target.id)
    if #disjink > 0 then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local disequip = room:askForDiscard(target, 1, 1, true, self.name, true, ".|.|.|.|.|equip", "#lm_xiandeng_disequip")
    if #disequip == 0 then
      room:damage{
        from = player,
        to = target,
        damage = 1,
        skillName = self.name,
      }
    end
  end,
}
local lm__yuejin = General(extension,"lm__yuejin","lm_k__ba",0)
-- lm__yuejin.cardinfo = "dismantlement"
lm__yuejin.hidden = true
LM.setlordServant(lm_weiwu,"lm__yuejin")
lm__yuejin:addSkill(lm_xiandeng)
lm__yuejin:addRelatedSkill(lm_caiyong__yuejin)
Fk:loadTranslationTable{
  ["lm__yuejin"] = "乐进",
  ["lm_xiandeng"] = "先登",
  ["#lm_xiandeng_disjink"] = "先登：你可以弃置一张【闪】，令 %src 选择弃置一张装备牌或受到你造成的1点伤害",
  ["#lm_xiandeng_disequip"] = "先登：你需弃置一张装备，否则受到1点伤害",
  [":lm_xiandeng"] = "其他角色回合开始时，你可以弃置一张【闪】，其选择弃置一张装备牌或受到你造成的1点伤害。",
  ["lm_caiyong__yuejin"] = "才用",
  [":lm_caiyong__yuejin"] = "视为使用【过河拆桥】。",
}

local lm_caiyong__xunyu = fk.CreateViewAsSkill{
  name = "lm_caiyong__xunyu",
  mute = true,
  pattern = "collateral",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("collateral")
    card.skillName = "lm_woyong"
    return card
  end,
  enabled_at_response = woyong_response
}
lm_caiyong__xunyu.trueName = "lm_caiyong"
local lm_juxian = fk.CreateTriggerSkill{
  name = "lm_juxian",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.AfterSkillEffect, LM.findingServant},
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(self) and player == target then
      if event == fk.AfterSkillEffect then
        return data.name == "lm_qiuxian" and player:usedSkillTimes(self.name,Player.HistoryPhase) == 0
      else
        return player:getMark("_lm_juxian-turn") ~= 0 and data.all_num
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterSkillEffect then
      player:addSkillUseHistory("lm_qiuxian", -1)
      player.room:setPlayerMark(player, "_lm_juxian-turn", 1)
    else
      data.all_num = 2
      player.room:setPlayerMark(player, "_lm_juxian-turn", 0)
    end
  end,
}
local lm__xunyu = General(extension,"lm__xunyu","lm_k__ba",0)
-- lm__xunyu.cardinfo = "collateral"
lm__xunyu.hidden = true
LM.setlordServant(lm_weiwu,"lm__xunyu")
lm__xunyu:addSkill(lm_juxian)
lm__xunyu:addRelatedSkill(lm_caiyong__xunyu)
Fk:loadTranslationTable{
  ["lm__xunyu"] = "荀彧",
  ["lm_juxian"] = "举贤",
  [":lm_juxian"] = "锁定技，每回合限一次，你发动〖求贤〗后不计入次数，然后你本回合下次发现将领时，抽取人数改为二。",
  ["lm_caiyong__xunyu"] = "才用",
  [":lm_caiyong__xunyu"] = "视为使用【借刀杀人】。",
}

local lm_caiyong__simayi = fk.CreateViewAsSkill{
  name = "lm_caiyong__simayi",
  mute = true,
  pattern = "lm_adaptation",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    if Self:getMark("lm_adaptation-turn") == 0 then return end
    local card = Fk:cloneCard(Self:getMark("lm_adaptation-turn"))
    card.skillName = "lm_woyong"
    return card
  end,
  enabled_at_response = woyong_response
}
lm_caiyong__simayi.trueName = "lm_caiyong"
local lm_qiangyun = fk.CreateTriggerSkill{
  name = "lm_qiangyun",
  anim_type = "offensive",
  events = {fk.TargetSpecifying},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and data.to == player.id and data.from ~= player.id and data.card and data.card.type == Card.TypeTrick
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data, "#lm_qiangyun_promt:::" .. data.card:toLogString())
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".|.|heart,diamond",
    }
    room:judge(judge)
    local result = judge.card
    if result.color == Card.Red then
      AimGroup:cancelTarget(data, data.to)
    end
  end,
}
local lm__simayi = General(extension,"lm__simayi","lm_k__ba",0)
-- lm__simayi.cardinfo = "lm_indulgence"
lm__simayi.hidden = true
LM.setlordServant(lm_weiwu,"lm__simayi")
lm__simayi:addSkill(lm_qiangyun)
lm__simayi:addRelatedSkill(lm_caiyong__simayi)
Fk:loadTranslationTable{
  ["lm__simayi"] = "司马懿",
  ["#lm_qiangyun_promt"] = "强运：你可以判定，若判定牌为红色，取消 %arg 对你的目标",
  ["lm_qiangyun"] = "强运",
  [":lm_qiangyun"] = "当其他角色使用锦囊牌指定你为目标时，你可以判定，若为红色则取消之。",
  ["lm_caiyong__simayi"] = "才用",
  [":lm_caiyong__simayi"] = "视为使用【随机应变】。",
}

local lm_caiyong__xiahoudun = fk.CreateViewAsSkill{
  name = "lm_caiyong__xiahoudun",
  mute = true,
  pattern = "lm_amazing_grace",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("lm_amazing_grace")
    card.skillName = "lm_woyong"
    return card
  end,
  enabled_at_response = woyong_response
}
lm_caiyong__xiahoudun.trueName = "lm_caiyong"
local lm_ganglie = fk.CreateTriggerSkill{
  name = "lm_ganglie",
  anim_type = "offensive",
  events = {fk.Damage},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and data.to == player and data.from and not data.from.dead
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if not data.from:isNude() then
      local cid = room:askForCardChosen(player, data.from, "he", self.name)
      room:throwCard({cid}, self.name, data.from, player)
    end
    if room:askForSkillInvoke(player, self.name, data,"#lm_ganglie_remove_svt:" .. data.from.id) then
      LM.removeServants(player,{"lm__xiahoudun"}, player, nil, self.name)
      room:damage{
        from = player,
        to = data.from,
        damage = 1,
        skillName = self.name,
      }
    end
  end,
}
local lm__xiahoudun = General(extension,"lm__xiahoudun","lm_k__ba",0)
-- lm__xiahoudun.cardinfo = "lm_amazing_grace"
lm__xiahoudun.hidden = true
LM.setlordServant(lm_weiwu,"lm__xiahoudun")
lm__xiahoudun:addSkill(lm_ganglie)
lm__xiahoudun:addRelatedSkill(lm_caiyong__xiahoudun)
Fk:loadTranslationTable{
  ["lm__xiahoudun"] = "夏侯惇",
  ["#lm_ganglie_remove_svt"] = "刚烈：你可以移除夏侯惇，对 %src 造成1点伤害",
  ["lm_ganglie"] = "刚烈",
  [":lm_ganglie"] = "当你受到伤害后，你可以弃置伤害来源一张牌，然后你可以移除此将领，对伤害来源造成1点伤害。",
  ["lm_caiyong__xiahoudun"] = "才用",
  [":lm_caiyong__xiahoudun"] = "视为使用【五谷丰登】。",
}

local lm_caiyong__xiahouyuan = fk.CreateViewAsSkill{
  name = "lm_caiyong__xiahouyuan",
  mute = true,
  pattern = "snatch",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("snatch")
    card.skillName = "lm_woyong"
    return card
  end,
  enabled_at_response = woyong_response
}
lm_caiyong__xiahouyuan.trueName = "lm_caiyong"
local lm_shensu = fk.CreateTriggerSkill{
  name = "lm_shensu",
  anim_type = "offensive",
  events = {fk.EventAcquireSkill,fk.TargetSpecified},
  can_trigger = function (self, event, target, player, data)
    if event == fk.EventAcquireSkill then
      return player == target and data == self
    else
      return player:hasSkill(self) and player == target and data.card and data.card.trueName == "slash" and player:getMark("lm_shensu_slash") > 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventAcquireSkill then
      -- p("==========获得神速前两张杀次数+2，无视距离，不可响应===========")
      room:setPlayerMark(player,"lm_shensu_slash",2)
    else
      data.disresponsiveList = table.map(player.room.alive_players, Util.IdMapper)
    end
  end,
  refresh_events = {fk.CardUseFinished},
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(self) and player == target and data.card and data.card.trueName == "slash" and player:getMark("lm_shensu_slash") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:removePlayerMark(player,"lm_shensu_slash")
  end
}
local lm_shensu_slash = fk.CreateTargetModSkill{
  name = "#lm_shensu_slash",
  -- residue_func = function(self, player, skill, scope)
  --   if player:hasSkill(lm_shensu.name) and skill.trueName == "slash_skill"
  --     and player:getMark("lm_shensu_slash") > 0 then
  --     return player:getMark("lm_shensu_slash") 
  --   end
  -- end,
  bypass_distances =  function(self, player, skill)
    return player:hasSkill(lm_shensu.name) and skill.trueName == "slash_skill" and player:getMark("lm_shensu_slash") > 0
  end,
}
local lm__xiahouyuan = General(extension,"lm__xiahouyuan","lm_k__ba",0)
-- lm__xiahouyuan.cardinfo = "snatch"
lm__xiahouyuan.hidden = true
LM.setlordServant(lm_weiwu,"lm__xiahouyuan")
lm_shensu:addRelatedSkill(lm_shensu_slash)
lm__xiahouyuan:addSkill(lm_shensu)
lm__xiahouyuan:addRelatedSkill(lm_caiyong__xiahouyuan)
Fk:loadTranslationTable{
  ["lm__xiahouyuan"] = "夏侯渊",
  ["#lm_shensu_slash"] = "神速",
  ["lm_shensu"] = "神速",
  [":lm_shensu"] = "你获得此将领后使用的前两张【杀】无距离限制且不可响应。",
  ["lm_caiyong__xiahouyuan"] = "才用",
  [":lm_caiyong__xiahouyuan"] = "视为使用【顺手牵羊】。",
}

local lm_caiyong__xuhuang = fk.CreateViewAsSkill{
  name = "lm_caiyong__xuhuang",
  mute = true,
  pattern = "lm_looting",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("lm_looting")
    card.skillName = "lm_woyong"
    return card
  end,
  enabled_at_response = woyong_response
}
lm_caiyong__xuhuang.trueName = "lm_caiyong"
local lm_duanliang = fk.CreateTriggerSkill{
  name = "lm_duanliang",
  anim_type = "offensive",
  events = {fk.EventPhaseStart,fk.DrawNCards},
  can_trigger = function (self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return player:hasSkill(self) and player ~= target and target.phase == Player.Draw
    else
      return player:hasSkill(self) and player ~= target and target:getMark("lm_duanliang_reduce") == 1
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local disslash = room:askForDiscard(player, 1, 1, false, self.name, true, "slash|.|.|.|.|", "#lm_duanliang_disslash:" .. target.id)
      if #disslash > 0 then
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local judge = {
        who = target,
        pattern = ".|.|^club",
        reason = self.name
      }
      room:judge(judge)
      local result = judge.card
      if result.suit ~= Card.Club then
        room:drawCards(player,1,self.name)
        room:setPlayerMark(target,"lm_duanliang_reduce",1)
      end
    else
      data.n = data.n - 1
    end
  end,
}
local lm__xuhuang = General(extension,"lm__xuhuang","lm_k__ba",0)
-- lm__xuhuang.cardinfo = "supply_shortage"
lm__xuhuang.hidden = true
LM.setlordServant(lm_weiwu,"lm__xuhuang")
lm__xuhuang:addSkill(lm_duanliang)
lm__xuhuang:addRelatedSkill(lm_caiyong__xuhuang)
Fk:loadTranslationTable{
  ["lm__xuhuang"] = "徐晃",
  ["lm_duanliang"] = "断粮",
  ["#lm_duanliang_disslash"] = "是否弃置一张杀令 %src 判定，若结果不为♣，则你摸一张牌，其少摸一张牌",
  -- ["@@lm_duanliang_reduce"] = "少摸1张",
  [":lm_duanliang"] = "其他角色的摸牌阶段，你可以弃置一张【杀】令其判定，若结果不为♣，则你摸一张牌，其少摸一张牌。",
  ["lm_caiyong__xuhuang"] = "才用",
  [":lm_caiyong__xuhuang"] = "视为使用【趁火打劫】。",
}

local lm_caiyong__yujin = fk.CreateViewAsSkill{
  name = "lm_caiyong__yujin",
  mute = true,
  pattern = "jink",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("jink")
    card.skillName = "lm_woyong"
    return card
  end,
  enabled_at_response = woyong_response
}
lm_caiyong__yujin.trueName = "lm_caiyong"
local lm_yanji = fk.CreateTriggerSkill{
  name = "lm_yanji",
  anim_type = "offensive",
  events = {fk.EventPhaseStart,fk.EventPhaseEnd},
  can_trigger = function (self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return player:hasSkill(self) and player == target and player.phase == Player.Discard
    elseif event == fk.EventPhaseEnd then
      return player:getMark("lm_yanji_invoke-phase") ~= 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      return room:askForSkillInvoke(player, self.name, data, "#lm_yanji_draw")
    else
      local count = 0
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if count > 0 then break end
          if move.from and move.from == player.id and fk.ReasonDiscard == move.moveReason and move.toArea and move.toArea == Card.DiscardPile then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                count = count + 1
              end
            end
          end
        end
      end, Player.HistoryPhase)
      return count == 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      room:drawCards(player,2,self.name)
      room:setPlayerMark(player,"lm_yanji_invoke-phase",1)
    else
      room:setPlayerMark(player,"lm_yanji_invoke-phase",0)
      LM.removeServants(player,{"lm__yujin"}, nil, nil, self.name)
    end
  end,
}
local lm_yanji_maxc = fk.CreateMaxCardsSkill{
  name = "#lm_yanji_maxc",
  correct_func = function(self, player)
    if player:hasSkill(lm_yanji) and player:isWounded() then
      return 1
    end
  end
}
local lm__yujin = General(extension,"lm__yujin","lm_k__ba",0)
-- lm__yujin.cardinfo = "jink"
lm__yujin.hidden = true
LM.setlordServant(lm_weiwu,"lm__yujin")
lm_yanji:addRelatedSkill(lm_yanji_maxc)
lm__yujin:addSkill(lm_yanji)
lm__yujin:addRelatedSkill(lm_caiyong__yujin)
Fk:loadTranslationTable{
  ["lm__yujin"] = "于禁",
  ["#lm_yanji_maxc"] = "严整",
  ["lm_yanji"] = "严整",
  [":lm_yanji"] = "弃牌阶段开始时，你摸两张牌，此阶段结束时，若你未弃置牌，则移除此将领。若你已受伤，你的手牌上限+1",
  ["#lm_yanji_draw"] = "是否摸两张牌，此阶段结束时若无弃牌则移除【于禁】将领",
  -- ["@@lm_yanji_reduce"] = "少摸1张",
  ["lm_caiyong__yujin"] = "才用",
  [":lm_caiyong__yujin"] = "视为使用【闪】。",
}

local lm_caiyong__xuchu = fk.CreateViewAsSkill{
  name = "lm_caiyong__xuchu",
  mute = true,
  pattern = "duel",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("duel")
    card.skillName = "lm_woyong"
    return card
  end,
  enabled_at_response = woyong_response
}
lm_caiyong__xuchu.trueName = "lm_caiyong"
local lm_luoyi = fk.CreateActiveSkill{
  name = "lm_luoyi",
  anim_type = "offensive",
  prompt = "#lm_luoyi",
  card_num = 1,
  card_filter = function(self, to_select, selected)
    local card = Fk:getCardById(to_select)
    return #selected == 0 and card and card.type == Card.TypeEquip and card.sub_type == Card.SubtypeArmor
  end,
  target_num = 1,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local cards = effect.cards
    room:throwCard(cards, self.name, player, player)
    LM.removeServants(player,{"lm__xuchu"}, player, nil, self.name)
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = self.name,
    }
    LM.removeServantsByChc(target, 1, nil, player, nil, self.name)
  end
}
local lm__xuchu = General(extension,"lm__xuchu","lm_k__ba",0)
-- lm__xuchu.cardinfo = "duel"
lm__xuchu.hidden = true
LM.setlordServant(lm_weiwu,"lm__xuchu")
lm__xuchu:addSkill(lm_luoyi)
lm__xuchu:addRelatedSkill(lm_caiyong__xuchu)
Fk:loadTranslationTable{
  ["lm__xuchu"] = "许褚",
  ["lm_luoyi"] = "裸衣",
  [":lm_luoyi"] = "出牌阶段，你可以弃置一张防具牌并移除此将领，对一名其他角色造成1点伤害，然后移除其一名将领。",
  ["#lm_luoyi"] = "你可以弃置一张防具牌并移除许褚，对一名其他角色造成1点伤害，然后移除其一名将领",
  ["lm_caiyong__xuchu"] = "才用",
  [":lm_caiyong__xuchu"] = "视为使用【决斗】。",
}

local lm_caiyong__xunyou = fk.CreateViewAsSkill{
  name = "lm_caiyong__xunyou",
  mute = true,
  pattern = "ex_nihilo",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard("ex_nihilo")
    card.skillName = "lm_woyong"
    return card
  end,
  enabled_at_response = woyong_response
}
lm_caiyong__xunyou.trueName = "lm_caiyong"
local lm__xunyou = General(extension,"lm__xunyou","lm_k__ba",0)
-- lm__xunyou.cardinfo = "ex_nihilo"
lm__xunyou.hidden = true
LM.setlordServant(lm_weiwu,"lm__xunyou")
lm__xunyou:addRelatedSkill(lm_caiyong__xunyou)
Fk:loadTranslationTable{
  ["lm__xunyou"] = "荀攸",
  ["lm_caiyong__xunyou"] = "才用",
  [":lm_caiyong__xunyou"] = "视为使用【无中生有】。",
}

--------------------------------------------------曹丕-------------------------------------------------------------
local lm_weiwen = fk.CreateTriggerSkill{
  name = "lm_weiwen",
  mute = true,
  events = {fk.GameStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local svts = table.simpleClone(self.lmLordList)
    -- table.shuffle(svts)
    local max = 3
    local num = 1

    svts = table.map(svts, function(s) return LM.initServant(player, s) end)
    LM.setServantsPool(player, svts)
    LM.setServantsMax(player, max)
    local init_svts = table.random(LM.getServantsPool(player), num)
    LM.findServants(player, table.map(init_svts, Util.NameMapper), self.name)
  end
}
local lm_dangbi = fk.CreateTriggerSkill{
  name = "lm_dangbi",
  frequency = Skill.Compulsory,
  events = {LM.importingServants, LM.afterServantsRemoved, fk.AfterSkillEffect},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == LM.importingServants then
        return #data.svts ~= 0
      elseif event == LM.afterServantsRemoved then
        return table.find(data.svts, function(svt) return svt.hide end)
      elseif event == fk.AfterSkillEffect then
        return LM.getServantFromSkill(player, data.name) ~= nil
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == LM.importingServants then
      for _, svt in ipairs(data.svts) do
        svt.hide = true
      end
    elseif event == LM.afterServantsRemoved then
      for _, svt in ipairs(data.svts) do
        svt.hide = nil --- 小心hide == false，先掐了
      end
    elseif event == fk.AfterSkillEffect then
      local svt = LM.getServantFromSkill(player, data.name)
      if svt then LM.setSvtHide(player, svt.name, nil) end
    end
  end,
}
local lm_songwei_tri = fk.CreateTriggerSkill{
  name = "#lm_songwei_tri",
  mute = true,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("lm_songwei") and data.skillName == "lm_songwei" and table.find(data.svts, function(svt) return svt.hide end)
  end,
  on_trigger = function(self, event, target, player, data)
    for _, svt in ipairs(data.svts) do
      if svt.hide then
        self:doCost(event, target, player, data)
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:notifySkillInvoked(player, "lm_songwei", "drawcard")
    player:drawCards(2, "lm_songwei")
  end
}
local lm_songwei = fk.CreateActiveSkill{
  name = "lm_songwei",
  prompt = "#lm_songwei",
  max_phase_use_time = 1,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 0,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    for _ = 1, 2, 1 do
      local judge = {
        who = player,
        reason = self.name,
        pattern = ".",
      }
      room:judge(judge)
      if judge.card then
        if judge.card.color == Card.Black and LM.canFindNewSvt(player) then
          LM.findServantsByChc(player, 1, 1, 3, self.name)
        elseif judge.card.color == Card.Red then
          local svt = LM.removeServantsByChc(player, 1, nil, player, nil, self.name)
          -- p(svt)
          -- if svt[1].hide then
          --   player:drawCards(2, self.name)
          -- end
        end
      end
    end
  end
}
local lm_jianan = fk.CreateTriggerSkill{
  name = "lm_jianan",
  anim_type = "control",
  events = {fk.AskForRetrial},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local prompt = "#lm_jianan-ask:::" .. data.reason
    local pattern = ".|.|.|.|.|trick"
    if player:hasSkill("lm_guizao") then
      player.room:useSkill(player, Fk.skills["lm_guizao"], Util.DummyFunc)
      prompt = "#lm_jianan-askX:::" .. data.reason
      pattern = "."
    end
    local card = player.room:askForCard(player, 1, 1, true, self.name, true, pattern, prompt)
    if #card ~= 0 then
      self.cost_data = card[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:retrial(Fk:getCardById(self.cost_data), player, data, self.name, true)
  end,
}
local lm__caopi = General(extension,"lm__caopi","lm_k__ba",5)
lm_songwei:addRelatedSkill(lm_songwei_tri)
lm__caopi:addSkill(lm_weiwen)
lm__caopi:addSkill(lm_dangbi)
lm__caopi:addSkill(lm_songwei)
lm__caopi:addSkill(lm_jianan)
Fk:loadTranslationTable{
  ["lm__caopi"] = "曹丕",
  ["lm_weiwen"] = "魏文",
  [":lm_weiwen"] = "军力技，你的初始将领上限为3。游戏开始时，你随机获得一名将领。",
  ["lm_dangbi"] = "党蔽",
  [":lm_dangbi"] = "锁定技，你获得将领时，暗置之；你的暗置将领发动技能后或被移除结束后，明置之。", --- 暗置：对其他角色不可见
  ["lm_songwei"] = "颂威",
  [":lm_songwei"] = "阶段技，你可以判定两次，黑色则发现一名将领；红色则移除一名将领，若为暗置将领，摸两张牌。",
  ["#lm_songwei"] = "你可以判定两次，黑色则发现一名将领；红色则移除一名将领，若为暗置将领，摸两张牌",
  ["lm_jianan"] = "建安",
  [":lm_jianan"] = "你的判定牌生效前，你可以用一张锦囊牌替换之。",
  ["#lm_jianan-ask"] = "建安：你可以用一张锦囊牌替换你的 “%arg” 判定",
  ["#lm_jianan-askX"] = "建安：你可以用一张牌替换你的 “%arg” 判定",
  ["@[lm_servants]&lm_weiwen"] = "将池",
  ["@[lm_servants]lm_weiwen"] = "将领",
}

local lm__caopi_caoren = General(extension,"lm__caopi_caoren","lm_k__ba",0)
LM.setlordServant(lm_weiwen, "lm__caopi_caoren")
lm__caopi_caoren.trueName = "caoren"
lm__caopi_caoren.hidden = true
local lm_lizhan = fk.CreateTriggerSkill{
  name = "lm_lizhan",
  anim_type = "drawcard",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:isWounded()
  end,
  on_use = function(self, event, target, player, data)
    if player.hp == 1 then
      player:drawCards(2, self.name)
    else
      player:drawCards(1, self.name)
    end
  end
}
lm__caopi_caoren:addSkill(lm_lizhan)
Fk:loadTranslationTable{
  ["lm__caopi_caoren"] = "曹仁",
  ["lm_lizhan"] = "励战",
  [":lm_lizhan"] = "回合结束时，若你已受伤，你可以摸一张牌，若你的体力值为1则多摸一张牌。",
}

local lm__caopi_simayi = General(extension,"lm__caopi_simayi","lm_k__ba",0)
LM.setlordServant(lm_weiwen, "lm__caopi_simayi")
lm__caopi_simayi.trueName = "simayi"
lm__caopi_simayi.hidden = true
local lm_zhabing = fk.CreateActiveSkill{
  name = "lm_zhabing",
  anim_type = "offensive",
  prompt = "#lm_zhabing",
  max_phase_use_time = 1,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 1,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    target:drawCards(1, self.name)
    room:damage{
      from = player,
      to = player,
      damage = 1,
      skillName = self.name
    }
    if player.dead then return end
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = self.name
    }
  end
}
lm__caopi_simayi:addSkill(lm_zhabing)
Fk:loadTranslationTable{
  ["lm__caopi_simayi"] = "司马懿",
  ["lm_zhabing"] = "诈病",
  [":lm_zhabing"] = "阶段技，你可以令一名其他角色摸一张牌，然后你对自己造成1点伤害，再对其造成1点伤害。",
  ["#lm_zhabing"] = "你可以令一名其他角色摸一张牌，然后你对自己造成1点伤害，再对其造成1点伤害",
}

local lm__guohuai = General(extension,"lm__guohuai","lm_k__ba",0)
LM.setlordServant(lm_weiwen, "lm__guohuai")
lm__guohuai.hidden = true
local lm_jingce = fk.CreateTriggerSkill{
  name = "lm_jingce",
  anim_type = "drawcard",
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target and target.phase == Player.Play and #player.room.logic:getActualDamageEvents(1, function(e)
      return e.data[1].to == player
    end, Player.HistoryPhase) ~= 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local ids = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            local card = Fk:getCardById(info.cardId)
            if card and card.type == Card.TypeTrick and room:getCardArea(info.cardId) == Card.DiscardPile then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
    end, Player.HistoryPhase)
    if #ids ~= 0 and #player.room:askForDiscard(player, 1, 1, true, self.name, true, nil, "#lm_jingce-invoke") ~= 0 then
      self.cost_data = ids
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = self.cost_data
    if #ids > 1 then
      local cards, _ = U.askforChooseCardsAndChoice(player, ids, {"OK"}, self.name,
      "#lm_jingce-choose", nil, 1, 2)
      if #cards > 0 then
        ids = cards
      end
    end
    room:moveCardTo(ids, Card.PlayerHand, player, fk.ReasonPrey, self.name)
  end
}
lm__guohuai:addSkill(lm_jingce)
Fk:loadTranslationTable{
  ["lm__guohuai"] = "郭淮",
  ["lm_jingce"] = "精策",
  [":lm_jingce"] = "一名角色出牌阶段结束时，若你本阶段受到过伤害，你可以弃置一张牌，获得本阶段进入弃牌堆的至多两张锦囊牌。",
  ["#lm_jingce-invoke"] = "精策：你可以弃置一张牌，获得本阶段进入弃牌堆的至多两张锦囊牌",
  ["#lm_jingce-choose"] = "精策：你须获得至多两张锦囊牌",
}

local lm__liufang = General(extension,"lm__liufang","lm_k__ba",0)
LM.setlordServant(lm_weiwen, "lm__liufang")
lm__liufang.hidden = true
local lm_guizao = fk.CreateTriggerSkill{
  name = "lm_guizao",
}
lm__liufang:addSkill(lm_guizao)
Fk:loadTranslationTable{
  ["lm__liufang"] = "刘放",
  ["lm_guizao"] = "瑰藻",
  [":lm_guizao"] = "〖建安〗的“锦囊牌”改成“牌”。",
}

local lm__sunzi = General(extension,"lm__sunzi","lm_k__ba",0)
LM.setlordServant(lm_weiwen, "lm__sunzi")
lm__sunzi.hidden = true
local lm_jiyu = fk.CreateTriggerSkill{
  name = "lm_jiyu",
  anim_type = "drawcard",
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.skillName == "lm_songwei" and table.find(data.svts, function(svt) return not svt.hide end)
  end,
  on_trigger = function(self, event, target, player, data)
    for _, svt in ipairs(data.svts) do
      if not svt.hide then
        self:doCost(event, target, player, data)
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:drawCards(2, self.name)
  end
}
lm__sunzi:addSkill(lm_jiyu)
Fk:loadTranslationTable{
  ["lm__sunzi"] = "孙资",
  ["lm_jiyu"] = "讥谀",
  [":lm_jiyu"] = "锁定技，你因〖颂威〗移除一个明置将领后，摸两张牌。",
}

local lm__caozhen = General(extension,"lm__caozhen","lm_k__ba",0)
LM.setlordServant(lm_weiwen, "lm__caozhen")
lm__caozhen.hidden = true
local lm_sidi = fk.CreateTriggerSkill{
  name = "lm_sidi",
  anim_type = "drawcard",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if not (player == target and player:hasSkill(self)) then return end
    if data.from and player.id ~= data.from and not player.room:getPlayerById(data.from).dead then
      local events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data[1]
        return use.from == data.from and use.card.type == Card.TypeTrick
      end, Player.HistoryTurn)
      if #events == 0 then
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(2, self.name)
  end
}
lm__caozhen:addSkill(lm_sidi)
Fk:loadTranslationTable{
  ["lm__caozhen"] = "曹真",
  ["lm_sidi"] = "司敌",
  [":lm_sidi"] = "你成为其他角色使用【杀】的目标后，若其本回合未使用过锦囊牌，你可以摸两张牌。",
}

local lm__caoxiu = General(extension,"lm__caoxiu","lm_k__ba",0)
LM.setlordServant(lm_weiwen, "lm__caoxiu")
lm__caoxiu.hidden = true
local lm_taoxi = fk.CreateTriggerSkill{
  name = "lm_taoxi",
  anim_type = "masochism",
  events = {fk.Damaged},
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local other = table.filter(room:getOtherPlayers(player, false), function(p) return not p:isNude() end)
    local victim = room:askForChoosePlayers(player, table.map(other, Util.IdMapper), 1, 1, "#lm_taoxi-invoke", self.name)
    if #victim ~= 0 then
      self.cost_data = victim[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local victim = room:getPlayerById(self.cost_data)
    local card_num = #victim:getCardIds("he")
    local cards = room:askForCard(victim, 1, card_num, true, self.name, false, nil, "#lm_taoxi-ask:" .. player.id)
    if #cards ~= 0 then
      room:obtainCard(player, cards, false, fk.ReasonGive, victim.id, self.name)
      if #cards == 2 then
        room:damage{
          from = victim,
          to = player,
          damage = 1,
          skillName = self.name
        }
      end
    end
  end
}
lm__caoxiu:addSkill(lm_taoxi)
Fk:loadTranslationTable{
  ["lm__caoxiu"] = "曹休",
  ["lm_taoxi"] = "讨袭",
  [":lm_taoxi"] = "当你受到伤害后，你可以令一名其他角色交给你至少一张牌，若你获得了两张牌，其对你造成1点伤害。",
  ["#lm_taoxi-invoke"] = "讨袭：你可以令一名其他角色交给你至少一张牌，若你获得了两张牌，其对你造成1点伤害",
  ["#lm_taoxi-ask"] = "讨袭：你须交给 %src 至少一张牌，若其获得了两张牌，你对其造成1点伤害",
}

local lm__chenqun = General(extension,"lm__chenqun","lm_k__ba",0)
LM.setlordServant(lm_weiwen, "lm__chenqun")
lm__chenqun.hidden = true
local lm_faen = fk.CreateTriggerSkill{
  name = "lm_faen",
  anim_type = "support",
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return player == target and player:hasSkill(self) and player.phase < Player.NotActive
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:recover{
      who = player,
      num = 2,
      recoverBy = player,
      skillName = self.name
    }
  end
}
lm__chenqun:addSkill(lm_faen)
Fk:loadTranslationTable{
  ["lm__chenqun"] = "陈群",
  ["lm_faen"] = "法恩",
  [":lm_faen"] = "你的回合内，你进入濒死状态后，你回复2点体力。",
}

local lm__caozhang = General(extension,"lm__caozhang","lm_k__ba",0)
LM.setlordServant(lm_weiwen, "lm__caozhang")
lm__caozhang.hidden = true
local lm_changqu = fk.CreateTriggerSkill{
  name = "lm_changqu",
  anim_type = "offensive",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and not player:isNude() and player:usedSkillTimes(self.name, Player.HistoryRound) == 0 and
    target and not target.dead and #player.room.logic:getActualDamageEvents(1, function(e)
      return e.data[1].to == player
    end, Player.HistoryTurn) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askForDiscard(player, 1, 1, true, self.name, true, nil, "#lm_changqu-invoke::" .. target.id)
    if #cards ~= 0 then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = self.name
    }
  end
}
lm__caozhang:addSkill(lm_changqu)
Fk:loadTranslationTable{
  ["lm__caozhang"] = "曹彰",
  ["lm_changqu"] = "长驱",
  [":lm_changqu"] = "轮次技，一名角色回合结束时，若你本回合未受到过伤害，你可以弃置一张牌并对其造成1点伤害。",
  ["#lm_changqu-invoke"] = "长驱：你可以弃置一张牌并对 %dest 造成1点伤害",
}

local lm__caopi_jiaxu = General(extension,"lm__caopi_jiaxu","lm_k__ba",0)
LM.setlordServant(lm_weiwen, "lm__caopi_jiaxu")
lm__caopi_jiaxu.trueName = "jiaxu"
lm__caopi_jiaxu.hidden = true
local lm_jianshu = fk.CreateTriggerSkill{
  name = "lm_jianshu",
  anim_type = "masochism",
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return player == target and player:hasSkill(self) and data.to and data.to == player
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local other = table.filter(room:getOtherPlayers(player, false), function(p) return #p:getCardIds("he") >= 2 end)
    local victim = room:askForChoosePlayers(player, table.map(other, Util.IdMapper), 1, 1, "#lm_jianshu-invoke", self.name)
    if #victim ~= 0 then
      self.cost_data = victim[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local victim = room:getPlayerById(self.cost_data)
    room:askForDiscard(victim, 2, 2, true, self.name, false)
  end
}
lm__caopi_jiaxu:addSkill(lm_jianshu)
Fk:loadTranslationTable{
  ["lm__caopi_jiaxu"] = "贾诩",
  ["lm_jianshu"] = "间书",
  [":lm_jianshu"] = "当你对自己造成伤害后，你可以令一名其他角色弃置两张牌。",
  ["#lm_jianshu-invoke"] = "间书：你可以令一名其他角色弃置两张牌",
}

local lm__jiangji = General(extension,"lm__jiangji","lm_k__ba",0)
LM.setlordServant(lm_weiwen, "lm__jiangji")
lm__jiangji.hidden = true
local lm_qinwang = fk.CreateTriggerSkill{
  name = "lm_qinwang",
  anim_type = "masochism",
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return player == target and player:hasSkill(self) and data.to and data.to == player
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(3, self.name)
  end
}
lm__jiangji:addSkill(lm_qinwang)
Fk:loadTranslationTable{
  ["lm__jiangji"] = "蒋济",
  ["lm_qinwang"] = "勤王",
  [":lm_qinwang"] = "当你对自己造成伤害后，你可以摸三张牌。",
}

local lm__wangling = General(extension,"lm__wangling","lm_k__ba",0)
LM.setlordServant(lm_weiwen, "lm__wangling")
lm__wangling.hidden = true
local lm_yinghuo = fk.CreateActiveSkill{
  name = "lm_yinghuo",
  prompt = "#lm_yinghuo",
  max_phase_use_time = 1,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 0,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    player:drawCards(1, self.name, "top", {"@@lm_yinghuo-turn-inhand", 1})
  end
}
local lm_yinghuo_vs = fk.CreateFilterSkill{
  name = "#lm_yinghuo_vs",
  card_filter = function(self, card, player, judge)
    return card:getMark("@@lm_yinghuo-turn-inhand") ~= 0
  end,
  view_as = function(self, card)
    return Fk:cloneCard("fire_attack", card.suit, card.number)
  end
}
lm_yinghuo:addRelatedSkill(lm_yinghuo_vs)
lm__wangling:addSkill(lm_yinghuo)
Fk:loadTranslationTable{
  ["lm__wangling"] = "王凌",
  ["lm_yinghuo"] = "萤惑",
  [":lm_yinghuo"] = "阶段技，你可以摸一张牌，本回合此牌视为【火攻】。",
  ["#lm_yinghuo"] = "你可以摸一张牌，本回合此牌视为【火攻】",
  ["#lm_yinghuo_vs"] = "萤惑",
  ["@@lm_yinghuo-turn-inhand"] = "萤惑",
}

--------------------------------------------------孙策-------------------------------------------------------------
local lm_zhiba = fk.CreateTriggerSkill{
  name = "lm_zhiba",
  mute = true,
  events = {fk.GameStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local svts = table.simpleClone(self.lmLordList)
    -- table.shuffle(svts)
    local max = 3
    local num = 2

    svts = table.map(svts, function(s) return LM.initServant(player, s) end)
    LM.setServantsPool(player, svts)
    LM.setServantsMax(player, max)
    local init_svts = table.random(LM.getServantsPool(player), num)
    LM.findServants(player, table.map(init_svts, Util.NameMapper), self.name)
  end
}
local lm_taoni = fk.CreateActiveSkill{
  name = "lm_taoni",
  anim_type = "offensive",
  prompt = "#lm_taoni",
  max_phase_use_time = 1,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 0,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    player:drawCards(1, self.name)
    local targets = table.filter(room:getOtherPlayers(player, false), function(p)
      return player:canPindian(p)
    end)
    if #targets == 0 then return end
    local tids = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#lm_taoni-pindian", self.name, true)
    if #tids == 0 then return end
    local target = room:getPlayerById(tids[1])
    local pindian = player:pindian({target}, self.name)
    if pindian.results[target.id].winner == player then
      LM.findServantsByChc(player, 1, 1, 3, self.name)
    else
      LM.removeServantsByChc(player, 1, nil, player, nil, self.name)
    end
    local tohand = #target:getCardIds("h")
    local playerhand = #player:getCardIds("h")
    if tohand > playerhand then
      player:drawCards(1, self.name)
    elseif tohand < playerhand then
      target:drawCards(1, self.name)
    end
  end
}
local lm_jiang = fk.CreateTriggerSkill{
  name = "lm_jiang",
  anim_type = "special",
  events ={fk.StartPindian},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if player == data.from then
        return data.fromCard == nil
      elseif table.contains(data.tos, player) then
        return not (data.results[player.id] and data.results[player.id].toCard)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local choices = {}
    local displayed = {}
    LM.getServantsByRule(player, function(svt)
      local g = Fk.generals[svt.name]
      if not g then return end
      return table.find(g.related_skills, function(s)
        if s.lmJiangNumber then
          table.insert(choices, svt.name)
          table.insert(displayed, "%arg（%arg2）:::" .. svt.name .. ":" .. Card:getNumberStr(s.lmJiangNumber))
          return true
        end
      end)
    end)
    table.insert(choices, "lm__sunce")
    table.insert(displayed, "%arg（%arg2）:::lm__sunce:K")
    table.insert(displayed, "Cancel")
    local choice = player.room:askForChoice(player, displayed, self.name, "#lm_jiang-invoke")
    if choice ~= "Cancel" then
      self.cost_data = choices[table.indexOf(displayed, choice)]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room

    -- 忍不了了，直接强制询问拼点！
    local extraData = {
      num = 1,
      min_num = 1,
      include_equip = false,
      pattern = ".",
      reason = data.reason,
    }
    local prompt = "#askForPindian:::" .. data.reason
    local requestdata = { "choose_cards_skill", prompt, false, extraData }

    local targets = {}
    if not data.fromCard then
      table.insert(targets, data.from)
    end
    for _, to in ipairs(data.tos) do
      if not (data.results[to.id] and data.results[to.id].toCard) then
        table.insert(targets, to)
      end
    end

    table.removeOne(targets, player)

    local req = Request:new(targets, "AskForUseActiveSkill")
    for _, p in ipairs(targets) do
      req:setData(p, requestdata)
      req:setDefaultReply(p, p:getCardIds(Player.Hand)[1])
    end
    req.focus_text = "AskForPindian"

    for _, p in ipairs(targets) do
      local _pindianCard
      local result = req:getResult(p)
      if type(result) == "table" then
        _pindianCard = Fk:getCardById(result.card.subcards[1])
      else
        _pindianCard = Fk:getCardById(result)
      end

      if p == data.from then
        data.fromCard = _pindianCard
      else
        data.results[p.id] = data.results[p.id] or {}
        data.results[p.id].toCard = _pindianCard
      end

      room:sendLog{
        type = "#ShowPindianCard",
        from = p.id,
        arg = _pindianCard:toLogString(),
      }
    end

    if self.cost_data == "lm__sunce" then
      local changed = "blank_shibing"
      LM.changeLord(player,"blank_shibing", false, false, false, false)
      if Fk.generals["sunce"] then
        changed = "sunce"
        room:setPlayerProperty(player, "general", "sunce")
        -- room:doAnimate("InvokeUltSkill", {
        --   name = "lm_jiang_anim",
        --   player = player.id,
        --   deputy = player.deputyGeneral and player.deputyGeneral ~= "" and table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), self.name),
        -- })
        local deputy = player.deputyGeneral and player.deputyGeneral ~= "" and table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), self.name)
        room:doSuperLightBox("packages/luming/qml/UltSkillAnimation.qml", {
          general = deputy and player.deputyGeneral or player.general,
          skill_name = "lm_jiang_anim"
        })
        player:chat("$hunzi:1")
        room:delay(2000)
      end
      room:sendLog{
        type = "#ChangeHero",
        from = player.id,
        arg = "lm__sunce",
        arg2 = changed,
        arg3 = "mainGeneral",
      }
      room:setPlayerMark(player, "_lm_jiang_number", 13)
    else
      local svt = LM.removeServants(player, {self.cost_data}, player, nil, self.name)
      if svt and svt[1] then
        local g = Fk.generals[svt[1].name]
        if g then
          for _, s in ipairs(g.related_skills) do
            if s.lmJiangNumber then
              room:setPlayerMark(player, "_lm_jiang_number", s.lmJiangNumber)
              break
            end
          end
        end
      end
    end
    room:setPlayerMark(player, "_lm_jiang_cardID", player.room.draw_pile[1])
    local _card = Fk:getCardById(player.room.draw_pile[1])
    if player == data.from then
      data.fromCard = _card
    else
      data.results[player.id] = data.results[player.id] or {}
      data.results[player.id].toCard = _card
    end
    local pindian = room.logic:getCurrentEvent():findParent(GameEvent.Pindian, true)
    if pindian then
      pindian:addExitFunc(function()
        room:setPlayerMark(player, "_lm_jiang_number", 0)
      end)
    end
  end,
  refresh_events = {fk.PindianCardsDisplayed},
  can_refresh = function(self, event, target, player, data)
    if player:getMark("_lm_jiang_number") == 0 then return end
    if player == data.from then
      return data._fromCard.id == player:getMark("_lm_jiang_cardID")
    elseif data.results[player.id] then
      return data.results[player.id]._toCard.id == player:getMark("_lm_jiang_cardID")
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local card, _card
    if player == data.from then
      card = data.fromCard
      _card = data._fromCard
    elseif data.results[player.id] then
      card = data.results[player.id].toCard
      _card = data.results[player.id]._toCard
    end
    local diff = card.number - _card.number
    card.number = math.max(math.min(player:getMark("_lm_jiang_number") + diff, 13), 0)
    player.room:sendLog{
      type = "#FilterCard",
      arg = "lm_jiang",
      from = player.id,
      arg2 = _card:toLogString(),
      arg3 = card:toLogString(),
    }
  end
}
local lm__sunce = General(extension,"lm__sunce","lm_k__ba",5)
lm__sunce:addSkill(lm_zhiba)
lm__sunce:addSkill(lm_taoni)
lm__sunce:addSkill(lm_jiang)
Fk:loadTranslationTable{
  ["lm__sunce"] = "孙策",
  ["lm_zhiba"] = "制霸",
  [":lm_zhiba"] = "军力技，你的初始将领上限为3。游戏开始时，你随机获得两名将领。",
  ["lm_taoni"] = "讨逆",
  [":lm_taoni"] = "阶段技，你可以摸一张牌，然后拼点，若你赢，你发现一名将领，否则你移除一名将领；然后拼点双方手牌数较少者摸一张牌。",
  ["#lm_taoni"] = "你可以摸一张牌，然后你可以拼点，若你赢，你发现一名将领，否则你移除一名将领；然后拼点双方手牌数较少者摸一张牌",
  ["lm_jiang"] = "激昂",
  [":lm_jiang"] = "你拼点时，你可以移除一名将领或用白板替换孙策当主公角色，若如此做，你令其他角色先决定拼点牌，然后你用牌堆顶牌拼点（若替换孙策则此牌点数视为K）。",
  ["#lm_taoni-pindian"] = "讨逆：你可以拼点，若你赢，你发现一名将领，否则你移除一名将领；然后拼点双方手牌数较少者摸一张牌",
  ["#lm_jiang-invoke"] = "激昂：你可以移除一名将领或自己，改为用牌堆顶牌拼点。",
  ["@[lm_servants]&lm_zhiba"] = "将池",
  ["@[lm_servants]lm_zhiba"] = "将领",

  ["lm_jiang_anim"] = "激昂",
  ["$lm_jiang_anim1"] = "父亲在上，魂佑江东；公瑾在旁，智定天下！",
  ["$lm_jiang_anim2"] = "愿承父志，与公瑾共谋天下！",
  ["$lm_jiang_anim3"] = "小霸王之名，响彻天下，何人不知！",
  ["$lm_jiang_anim4"] = "江东已平，中原动荡，直取许昌！",
  ["$lm_jiang_anim5"] = "平定三郡，并非难事，哈哈哈哈哈哈！",
  ["$lm_jiang_anim6"] = "开创我吴大业，正赖卿等相扶！",
  ["$lm_jiang_anim7"] = "父亲在上，公瑾在旁；佑我江东，国运盛强！",
  ["$lm_jiang_anim8"] = "属于我们的时代开始了！",
  ["$lm_jiang_anim9"] = "父兵诛卓起长沙，直取江东做帝家！",
  ["$lm_jiang_anim10"] = "所当者破，所击者服！",

  -- 真·激昂
  ["$lm_jiang_anim11"] = "我会把胜利带回江东！",
  ["$lm_jiang_anim12"] = "天下英雄，谁能与我一战？",
  ["$lm_jiang_anim13"] = "广阔天地，任我驰骋！",
  ["$lm_jiang_anim14"] = "江东广袤之地，必大有作为！",
  ["$lm_jiang_anim15"] = "剑锋所指，敌莫敢当！",
  ["$lm_jiang_anim16"] = "所向披靡，进退自如！",
  ["$lm_jiang_anim17"] = "吾等戮力同心，定能回天转日！",
  ["$lm_jiang_anim18"] = "建功立业、封妻荫子，正其时也！",

  --经典台词
  ["$lm_jiang_anim19"] = "孙家天下孙家兵，成就千秋万世名！",
}

local lm__zhouyu = General(extension,"lm__zhouyu","lm_k__ba",0)
LM.setlordServant(lm_zhiba, "lm__zhouyu")
lm__zhouyu.hidden = true
local lm_angyang__zhouyu = fk.CreateTriggerSkill{
  name = "lm_angyang__zhouyu",
  frequency = Skill.Compulsory,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.skillName == "lm_jiang" and table.find(data.svts, function(svt) return svt.name == "lm__zhouyu" end)
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_lm_jiang_number", 12)
  end,
}
lm_angyang__zhouyu.lmJiangNumber = 12
local lm_shengqi = fk.CreateTriggerSkill{
  name = "lm_shengqi",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.PindianCardsDisplayed, fk.PindianFinished},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return end
    return player == data.from or table.contains(data.tos, player)
  end,
  on_cost = function(self, event, target, player, data)
    if player == data.from then
      if event == fk.PindianCardsDisplayed then
        return data.fromCard.number < 13
      else
        for _, dat in pairs(data.results) do
          if dat.winner == player then
            return data.fromCard.number > 10
          end
        end
        return false
      end
    elseif data.results[player.id] then
      if event == fk.PindianCardsDisplayed then
        return data.results[player.id].toCard.number < 13
      else
        return data.results[player.id].winner == player and data.results[player.id].toCard.number > 10
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.PindianCardsDisplayed then
      if player == data.from then
        data.fromCard.number = math.min(data.fromCard.number + 1, 13)
      else
        data.results[player.id].toCard.number = math.min(data.results[player.id].toCard.number + 1, 13)
      end
    else
      player:drawCards(2, self.name)
    end
  end
}
lm__zhouyu:addRelatedSkill(lm_angyang__zhouyu)
lm__zhouyu:addSkill(lm_shengqi)
Fk:loadTranslationTable{
  ["lm__zhouyu"] = "周瑜",
  ["lm_angyang__zhouyu"] = "昂扬",
  [":lm_angyang__zhouyu"] = "锁定技，此将领因〖激昂〗被移除后，用牌堆顶牌当拼点牌时点数视为Q。",
  ["lm_shengqi"] = "盛气",
  [":lm_shengqi"] = "锁定技，你的拼点点数+1；你拼点赢后，若你的点数大于10，你摸两张牌。",
}

local lm__taishici = General(extension,"lm__taishici","lm_k__ba",0)
LM.setlordServant(lm_zhiba, "lm__taishici")
lm__taishici.hidden = true
local lm_angyang__taishici = fk.CreateTriggerSkill{
  name = "lm_angyang__taishici",
  frequency = Skill.Compulsory,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.skillName == "lm_jiang" and table.find(data.svts, function(svt) return svt.name == "lm__taishici" end)
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_lm_jiang_number", 11)
  end,
}
lm_angyang__taishici.lmJiangNumber = 11
local lm_hanzhan = fk.CreateTriggerSkill{
  name = "lm_hanzhan",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.AfterSkillEffect},
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(self) and player == target then
      return data.name == "lm_taoni" and player:usedSkillTimes(self.name,Player.HistoryPhase) == 0
    end
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillUseHistory("lm_taoni", -1)
  end,
}
lm__taishici:addRelatedSkill(lm_angyang__taishici)
lm__taishici:addSkill(lm_hanzhan)
Fk:loadTranslationTable{
  ["lm__taishici"] = "太史慈",
  ["lm_angyang__taishici"] = "昂扬",
  [":lm_angyang__taishici"] = "锁定技，此将领因〖激昂〗被移除后，用牌堆顶牌当拼点牌时点数视为J。",
  ["lm_hanzhan"] = "酣战",
  [":lm_hanzhan"] = "锁定技，每回合限一次，你发动〖讨逆〗后不计入次数。",
}

local lm__zhoutai = General(extension,"lm__zhoutai","lm_k__ba",0)
LM.setlordServant(lm_zhiba, "lm__zhoutai")
lm__zhoutai.hidden = true
local lm_angyang__zhoutai = fk.CreateTriggerSkill{
  name = "lm_angyang__zhoutai",
  frequency = Skill.Compulsory,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.skillName == "lm_jiang" and table.find(data.svts, function(svt) return svt.name == "lm__zhoutai" end)
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_lm_jiang_number", 10)
  end,
}
lm_angyang__zhoutai.lmJiangNumber = 10
local lm_xuezhan = fk.CreateActiveSkill{
  name = "lm_xuezhan",
  anim_type = "offensive",
  prompt = "#lm_xuezhan",
  max_phase_use_time = 1,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 1,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id and #LM.getServants(Fk:currentRoom():getPlayerById(to_select)) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = self.name
    }
    if target.dead then return end
    local svt = LM.getServantFromSkill(player, self.name)
    if svt then
      if #room:askForDiscard(target, 2, 2, true, self.name, true, nil, "#lm_xuezhan-discard:" .. player.id .. "::" .. svt.name) ~= 0 then
        LM.removeServants(player, {svt.name}, target, nil, self.name)
      end
    end
  end
}
lm__zhoutai:addRelatedSkill(lm_angyang__zhoutai)
lm__zhoutai:addSkill(lm_xuezhan)
Fk:loadTranslationTable{
  ["lm__zhoutai"] = "周泰",
  ["lm_angyang__zhoutai"] = "昂扬",
  [":lm_angyang__zhoutai"] = "锁定技，此将领因〖激昂〗被移除后，用牌堆顶牌当拼点牌时点数视为10。",
  ["lm_xuezhan"] = "血战",
  [":lm_xuezhan"] = "阶段技，你可以对一名没有将领的其他角色造成1点伤害，然后其可以弃置两张牌移除此将领。",
  ["#lm_xuezhan"] = "你可以对一名没有将领的其他角色造成1点伤害，然后其可以弃置两张牌移除周泰",
  ["#lm_xuezhan-discard"] = "血战：你可以弃置两张牌，然后 %src 移除 %arg",
}

local lm__chengpu = General(extension,"lm__chengpu","lm_k__ba",0)
LM.setlordServant(lm_zhiba, "lm__chengpu")
lm__chengpu.hidden = true
local lm_angyang__chengpu = fk.CreateTriggerSkill{
  name = "lm_angyang__chengpu",
  frequency = Skill.Compulsory,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.skillName == "lm_jiang" and table.find(data.svts, function(svt) return svt.name == "lm__chengpu" end)
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_lm_jiang_number", 9)
  end,
}
lm_angyang__chengpu.lmJiangNumber = 9
local lm_lihuo = fk.CreateViewAsSkill{
  name = "lm_lihuo",
  anim_type = "offensive",
  prompt = "#lm_lihuo",
  pattern = "fire_attack",
  card_num = 0,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local c = Fk:cloneCard("fire_attack")
    c.skillName = self.name
    return c
  end,
  before_use = function(self, player, use)
    use.additionalDamage = (use.additionalDamage or 0) + 1
  end,
  after_use = function(self, player, use)
    local room = player.room
    room:loseHp(player, 1, self.name)
    if player.dead or not use.damageDealt then return end
    local svt = LM.getServantFromSkill(player, self.name)
    if svt then
      LM.removeServants(player, {svt.name}, nil, nil, self.name)
    end
  end,
  enabled_at_play = LM.phaseCanUse,
  enabled_at_response = Util.FalseFunc
}
lm__chengpu:addRelatedSkill(lm_angyang__chengpu)
lm__chengpu:addSkill(lm_lihuo)
Fk:loadTranslationTable{
  ["lm__chengpu"] = "程普",
  ["lm_angyang__chengpu"] = "昂扬",
  [":lm_angyang__chengpu"] = "锁定技，此将领因〖激昂〗被移除后，用牌堆顶牌当拼点牌时点数视为9。",
  ["lm_lihuo"] = "疠火",
  [":lm_lihuo"] = "阶段技，你可以视为使用伤害+1的【火攻】，然后你失去1点体力，若造成伤害，则移除此将领。",
  ["#lm_lihuo"] = "你可以视为使用伤害+1的【火攻】，然后你失去1点体力，若造成了伤害，则移除此将领",
}

local lm__huanggai = General(extension,"lm__huanggai","lm_k__ba",0)
LM.setlordServant(lm_zhiba, "lm__huanggai")
lm__huanggai.hidden = true
local lm_angyang__huanggai = fk.CreateTriggerSkill{
  name = "lm_angyang__huanggai",
  frequency = Skill.Compulsory,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.skillName == "lm_jiang" and table.find(data.svts, function(svt) return svt.name == "lm__huanggai" end)
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_lm_jiang_number", 8)
  end,
}
lm_angyang__huanggai.lmJiangNumber = 8
local lm_huchen = fk.CreateTriggerSkill{
  name = "lm_huchen",
  anim_type = "offensive",
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.to and data.to ~= player
  end,
  on_cost = function(self, event, target, player, data)
    if #data.to:getCardIds{Player.Equip} == 0 then return end
    return player.room:askForSkillInvoke(player, self.name, nil, "#lm_huchen-invoke::"..data.to.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = room:askForCardChosen(player, data.to, "e", self.name)
    room:obtainCard(player.id, card, true, fk.ReasonPrey)
  end
}
lm__huanggai:addRelatedSkill(lm_angyang__huanggai)
lm__huanggai:addSkill(lm_huchen)
Fk:loadTranslationTable{
  ["lm__huanggai"] = "黄盖",
  ["lm_angyang__huanggai"] = "昂扬",
  [":lm_angyang__huanggai"] = "锁定技，此将领因〖激昂〗被移除后，用牌堆顶牌当拼点牌时点数视为8。",
  ["lm_huchen"] = "虎臣",
  [":lm_huchen"] = "当你对其他角色造成伤害后，你可以获得其装备区的一张牌。",
  ["#lm_huchen-invoke"] = "虎臣：你可以获得 %dest 装备区的一张牌。",
}

local lm__handang = General(extension,"lm__handang","lm_k__ba",0)
LM.setlordServant(lm_zhiba, "lm__handang")
lm__handang.hidden = true
local lm_angyang__handang = fk.CreateTriggerSkill{
  name = "lm_angyang__handang",
  frequency = Skill.Compulsory,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.skillName == "lm_jiang" and table.find(data.svts, function(svt) return svt.name == "lm__handang" end)
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_lm_jiang_number", 7)
  end,
}
lm_angyang__handang.lmJiangNumber = 7
local lm_qishe = fk.CreateTriggerSkill{
  name = "lm_qishe",
  anim_type = "offensive",
  events = {fk.TurnStart},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player == target
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if #player:getEquipments(Card.SubtypeOffensiveRide) == 0 and #player:getEquipments(Card.SubtypeDefensiveRide) == 0 then return end
    local slash = U.askForUseVirtualCard(room, player, "slash", nil, self.name, "#lm_qishe-invoke", true, false, true, false, nil, true)
    if slash then
      self.cost_data = slash
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:useCard(self.cost_data)
  end,
}
lm__handang:addRelatedSkill(lm_angyang__handang)
lm__handang:addSkill(lm_qishe)
Fk:loadTranslationTable{
  ["lm__handang"] = "韩当",
  ["lm_angyang__handang"] = "昂扬",
  [":lm_angyang__handang"] = "锁定技，此将领因〖激昂〗被移除后，用牌堆顶牌当拼点牌时点数视为7。",
  ["lm_qishe"] = "骑射",
  [":lm_qishe"] = "回合开始时，若你装备区有坐骑牌，你可以视为使用一张无视距离的【杀】。",
  ["#lm_qishe-invoke"] = "骑射：你可以视为使用一张无视距离的【杀】。",
}

local lm__jiangqin = General(extension,"lm__jiangqin","lm_k__ba",0)
LM.setlordServant(lm_zhiba, "lm__jiangqin")
lm__jiangqin.hidden = true
local lm_angyang__jiangqin = fk.CreateTriggerSkill{
  name = "lm_angyang__jiangqin",
  frequency = Skill.Compulsory,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.skillName == "lm_jiang" and table.find(data.svts, function(svt) return svt.name == "lm__jiangqin" end)
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_lm_jiang_number", 6)
  end,
}
lm_angyang__jiangqin.lmJiangNumber = 6
local lm_zhejie = fk.CreateTriggerSkill{
  name = "lm_zhejie",
  anim_type = "drawcard",
  events = {fk.PindianFinished},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or player:getMark("_lm_jiang_number") == 0 then return end
    if player == data.from then
      for _, dat in pairs(data.results) do
        if dat.winner == player then
          return player:getMark("_lm_jiang_number") < 7
        end
      end
      return false
    elseif data.results[player.id] then
      return data.results[player.id].winner == player and player:getMark("_lm_jiang_number") < 7
    end
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(3, self.name)
  end
}
lm__jiangqin:addRelatedSkill(lm_angyang__jiangqin)
lm__jiangqin:addSkill(lm_zhejie)
Fk:loadTranslationTable{
  ["lm__jiangqin"] = "蒋钦",
  ["lm_angyang__jiangqin"] = "昂扬",
  [":lm_angyang__jiangqin"] = "锁定技，此将领因〖激昂〗被移除后，用牌堆顶牌当拼点牌时点数视为6。",
  ["lm_zhejie"] = "折节",
  [":lm_zhejie"] = "你拼点赢后，若你于期间因〖激昂〗移除了〖昂扬〗点数小于7的将领，你可以摸三张牌。",
}

local lm__dongxi = General(extension,"lm__dongxi","lm_k__ba",0)
LM.setlordServant(lm_zhiba, "lm__dongxi")
lm__dongxi.hidden = true
local lm_angyang__dongxi = fk.CreateTriggerSkill{
  name = "lm_angyang__dongxi",
  frequency = Skill.Compulsory,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.skillName == "lm_jiang" and table.find(data.svts, function(svt) return svt.name == "lm__dongxi" end)
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_lm_jiang_number", 5)
  end,
}
lm_angyang__dongxi.lmJiangNumber = 5
local lm_fenming = fk.CreateTriggerSkill{
  name = "lm_fenming",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if target and target:isAlive() and target ~= player and target.phase == Player.Discard then
      return player:hasSkill(self) and player:canPindian(target)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForSkillInvoke(player, self.name, nil, "#lm_fenming-invoke:" .. target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local pindian = player:pindian({target}, self.name)
    if pindian.results[target.id] and pindian.results[target.id].winner == player then
      local phase = room.logic:getCurrentEvent():findParent(GameEvent.Phase, true) ---@type GameEvent
      if phase ~= nil then
        local cards = {}
        phase:addExitFunc(function()
          local end_id = phase.id
          room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
            for _, move in ipairs(e.data) do
              if move.from == target.id and move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard then
                for _, info in ipairs(move.moveInfo) do
                  if room:getCardArea(info.cardId) == Card.DiscardPile then
                    table.insertIfNeed(cards, info.cardId)
                  end
                end
              end
            end
            return false
          end, end_id)
          if #cards ~= 0 then
            room:obtainCard(player, cards, true, fk.ReasonPrey, player.id, self.name)
          end
        end)
      end
    end
  end
}
lm__dongxi:addRelatedSkill(lm_angyang__dongxi)
lm__dongxi:addSkill(lm_fenming)
Fk:loadTranslationTable{
  ["lm__dongxi"] = "董袭",
  ["lm_angyang__dongxi"] = "昂扬",
  [":lm_angyang__dongxi"] = "锁定技，此将领因〖激昂〗被移除后，用牌堆顶牌当拼点牌时点数视为5。",
  ["lm_fenming"] = "奋命",
  [":lm_fenming"] = "其他角色弃牌阶段开始时，你可以与其拼点，若你赢，本阶段结束后你获得其本阶段弃置的所有牌。",
  ["#lm_fenming-invoke"] = "奋命：你可以与 %src 拼点，若你赢，本阶段结束后你获得其本阶段弃置的所有牌。",
}

local lm__chenwu = General(extension,"lm__chenwu","lm_k__ba",0)
LM.setlordServant(lm_zhiba, "lm__chenwu")
lm__chenwu.hidden = true
local lm_angyang__chenwu = fk.CreateTriggerSkill{
  name = "lm_angyang__chenwu",
  frequency = Skill.Compulsory,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.skillName == "lm_jiang" and table.find(data.svts, function(svt) return svt.name == "lm__chenwu" end)
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_lm_jiang_number", 4)
  end,
}
lm_angyang__chenwu.lmJiangNumber = 4
local lm_cwduanxie = fk.CreateMaxCardsSkill{
  name = "lm_cwduanxie",
  correct_func = function(self, player)
    local room = Fk:currentRoom()
    local ret = 0
    for _, p in ipairs(room.players) do
      if p ~= player and p:hasSkill(self) then
        if p.hp < player.hp then
          ret = ret - 2
        else
          ret = ret - 1
        end
      end
    end
    return ret
  end
}
lm__chenwu:addRelatedSkill(lm_angyang__chenwu)
lm__chenwu:addSkill(lm_cwduanxie)
Fk:loadTranslationTable{
  ["lm__chenwu"] = "陈武",
  ["lm_angyang__chenwu"] = "昂扬",
  [":lm_angyang__chenwu"] = "锁定技，此将领因〖激昂〗被移除后，用牌堆顶牌当拼点牌时点数视为4。",
  ["lm_cwduanxie"] = "断绁",
  [":lm_cwduanxie"] = "锁定技，其他角色的手牌上限始终-1，体力大于你则改为-2。",
}

local lm__zhuzhi = General(extension,"lm__zhuzhi","lm_k__ba",0)
LM.setlordServant(lm_zhiba, "lm__zhuzhi")
lm__zhuzhi.hidden = true
local lm_angyang__zhuzhi = fk.CreateTriggerSkill{
  name = "lm_angyang__zhuzhi",
  frequency = Skill.Compulsory,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.skillName == "lm_jiang" and table.find(data.svts, function(svt) return svt.name == "lm__zhuzhi" end)
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_lm_jiang_number", 3)
  end,
}
lm_angyang__zhuzhi.lmJiangNumber = 3
local lm_anguo = fk.CreateTriggerSkill{
  name = "lm_anguo",
  anim_type = "drawcard",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(self)) then return end
    for _, p in ipairs(player.room.alive_players) do
      if LM.getServantSlot(p) == 0 then
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local fulls = 0
    for _, p in ipairs(player.room.alive_players) do
      if LM.getServantSlot(p) == 0 then
        player:drawCards(2, self.name)
      end
    end
  end
}
lm__zhuzhi:addRelatedSkill(lm_angyang__zhuzhi)
lm__zhuzhi:addSkill(lm_anguo)
Fk:loadTranslationTable{
  ["lm__zhuzhi"] = "朱治",
  ["lm_angyang__zhuzhi"] = "昂扬",
  [":lm_angyang__zhuzhi"] = "锁定技，此将领因〖激昂〗被移除后，用牌堆顶牌当拼点牌时点数视为3。",
  ["lm_anguo"] = "安国",
  [":lm_anguo"] = "回合结束时，每有一名满将领的角色，你便摸两张牌。",
}

local lm__sunben = General(extension,"lm__sunben","lm_k__ba",0)
LM.setlordServant(lm_zhiba, "lm__sunben")
lm__sunben.hidden = true
local lm_angyang__sunben = fk.CreateTriggerSkill{
  name = "lm_angyang__sunben",
  frequency = Skill.Compulsory,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.skillName == "lm_jiang" and table.find(data.svts, function(svt) return svt.name == "lm__sunben" end)
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_lm_jiang_number", 2)
  end,
}
lm_angyang__sunben.lmJiangNumber = 2
local lm_zhuiji = fk.CreateTriggerSkill{
  name = "lm_zhuiji",
  anim_type = "offensive",
  events = {fk.PindianFinished},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or player:isKongcheng() then return end
    if player == data.from then
      for _, dat in pairs(data.results) do
        return dat.winner == player
      end
      return false
    elseif data.results[player.id] then
      return data.results[player.id].winner == player
    end
  end,
  on_cost = function(self, event, target, player, data)
    local success, dat = player.room:askForUseActiveSkill(player, "#lm_zhuiji_viewas", "#lm_zhuiji-invoke", true)
    if success then
      self.cost_data = dat
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = Fk.skills["#lm_zhuiji_viewas"]:viewAs(self.cost_data.cards)
    local use = {
      from = player.id,
      tos = table.map(self.cost_data.targets, function(id) return {id} end),
      card = card,
      extraUse = true,
    }
    room:useCard(use)
  end
}
local lm_zhuiji_viewas = fk.CreateViewAsSkill{
  name = "#lm_zhuiji_viewas",
  card_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard("slash")
    card:addSubcards(cards)
    card.skillName = "lm_zhuiji"
    return card
  end,
}
Fk:addSkill(lm_zhuiji_viewas)
lm__sunben:addRelatedSkill(lm_angyang__sunben)
lm__sunben:addSkill(lm_zhuiji)
Fk:loadTranslationTable{
  ["lm__sunben"] = "孙贲",
  ["lm_angyang__sunben"] = "昂扬",
  [":lm_angyang__sunben"] = "锁定技，此将领因〖激昂〗被移除后，用牌堆顶牌当拼点牌时点数视为2。",
  ["lm_zhuiji"] = "追击",
  [":lm_zhuiji"] = "你拼点赢后，可以将一张牌当不计入次数的【杀】使用。",
  ["#lm_zhuiji_viewas"] = "追击",
  ["#lm_zhuiji-invoke"] = "追击：你可以将一张牌当不计入次数的【杀】使用。",
}

local lm__lvfan = General(extension,"lm__lvfan","lm_k__ba",0)
LM.setlordServant(lm_zhiba, "lm__lvfan")
lm__lvfan.hidden = true
local lm_angyang__lvfan = fk.CreateTriggerSkill{
  name = "lm_angyang__lvfan",
  frequency = Skill.Compulsory,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return data.skillName == "lm_jiang" and table.find(data.svts, function(svt) return svt.name == "lm__lvfan" end)
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_lm_jiang_number", 1)
  end,
}
lm_angyang__lvfan.lmJiangNumber = 1
local lm_diancai = fk.CreateViewAsSkill{
  name = "lm_diancai",
  anim_type = "drawcard",
  pattern = "ex_nihilo",
  card_num = 1,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local c = Fk:cloneCard("ex_nihilo")
    c:addSubcards(cards)
    c.skillName = self.name
    return c
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name) == 0
  end,
  enabled_at_response = function(self, player, response)
    return not response and player:usedSkillTimes(self.name) == 0
  end
}
local lm_diancai_trigger = fk.CreateTriggerSkill{
  name = "#lm_diancai_trigger",
  mute = true,
  events = {fk.BeforeDrawCard},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player and data.skillName == "ex_nihilo" then
      local room = player.room
      local c_event = room.logic:getCurrentEvent():findParent(GameEvent.CardEffect, true)
      if c_event == nil then return false end
      return c_event.data[1].card.trueName == "ex_nihilo" and c_event.data[1].from == player.id
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:notifySkillInvoked(player, "lm_diancai")
    data.num = (data.num or 0) + 1
  end,
}
lm_diancai:addRelatedSkill(lm_diancai_trigger)
lm__lvfan:addRelatedSkill(lm_angyang__lvfan)
lm__lvfan:addSkill(lm_diancai)
Fk:loadTranslationTable{
  ["lm__lvfan"] = "吕范",
  ["lm_angyang__lvfan"] = "昂扬",
  [":lm_angyang__lvfan"] = "锁定技，此将领因〖激昂〗被移除后，用牌堆顶牌当拼点牌时点数视为A。",
  ["lm_diancai"] = "典财",
  [":lm_diancai"] = "回合技，你可以将装备牌当【无中生有】使用；你使用【无中生有】多摸一张牌。",
}

return extension